{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#线性回归模型"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "现在，我们来做一些建模！我们从最简单的线性回归（Linear regression）开始。线性回归是最早的也是最基本的模型——把数据拟合成一条直线。\n",
    "\n",
    "<!-- TEASER_END -->"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##Getting ready"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "`boston`数据集很适合用来演示线性回归。`boston`数据集包含了波士顿地区的房屋价格中位数。还有一些可能会影响房价的因素，比如犯罪率（crime rate）。\n",
    "\n",
    "首先，让我们加载数据："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from sklearn import datasets\n",
    "boston = datasets.load_boston()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##How to do it..."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "实际上，用scikit-learn的线性回归非常简单，其API和前面介绍的模型一样。\n",
    "\n",
    "首先，导入`LinearRegression`类创建一个对象："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from sklearn.linear_model import LinearRegression\n",
    "lr = LinearRegression()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "现在，再把自变量和因变量传给`LinearRegression`的`fit`方法："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "LinearRegression(copy_X=True, fit_intercept=True, n_jobs=1, normalize=False)"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "lr.fit(boston.data, boston.target)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "现在开始预测："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "predictions = lr.predict(boston.data)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "要看预测的效果，我们用预测值与实际值的残差（residuals）直方图分布来演示："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAewAAAFsCAYAAAAOmoq4AAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAIABJREFUeJzt3XucnVV97/HPL+FOCAGik3CJg1GKoBgsKrUqAwakETEF\ny0WgwePLvqwcajznVEE9dFJbAasvU1q02iKmIBfRCsg1OeAIoUKkEsRwiegZDiCZCGQIF0mC+Z0/\n9pNxMpmZvSeZncma+bxfr/2a57L286xZycx31nqeZ+3ITCRJ0rZt3EhXQJIk1WdgS5JUAANbkqQC\nGNiSJBXAwJYkqQAGtiRJBTCwNaZFxM8j4t0jXY+RFBF/GhGPR8TzEfHmrXTOcyPiXwfZ3xkR7xmG\n85wZEXdu6XGkbYGBrVGrv1/6fX+BZ+YbM/OOOsdpjYj1ETFaf16+BHw8M3fLzPv77qy+9xeqQH8y\nIi6KiO225ISZeX5mfnSwItVLUmW0/gKSYPh/6ccwHuv3B40Y34zjNnjuAKYBD9Ypekhm7ga8GzgB\n+Itm103SxgxsjTUbBXjVCz+qWn5bRNwbEc9FxIqI+FJVbEMPvLvqZb49aj5Xvb8rIhZExMRex/3z\niHgsIp7uVW7Dedoj4rsRcVlEPAfMiYi3RsSPI2JVRPw6Iv4pIrbvdbz1EfGXEfGLiFgdEX8bEdOr\n93RHxFW9y/f5Hvuta0TsCDwPjAfuj4hf1G28zF8CdwEH9Tr+cRGxtKr7XRHxpl77Ph0RT1R1frhP\nG1zWq9wZvdrrM33q/62I+Hyv9baIeLzX+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9vf/vbvPe97x1wf1tbG5dccskW\nn6ejo4P99ttvi4/T22677UZnZ+ewHnOkedOZJG2muXPb6e5u3vEnTYL589sbKtva2srKlSsZP348\nu+66K0cffTQXX3wxEydO3Ozzn3baaZx22mkD7o+IEb17/swzz2S//fbj85///Cb7nn/++RGoUXMZ\n2JK0mbq7obW1vWnH7+xs/NgRwQ033MBRRx1FV1cX733ve/m7v/s7vvjFvhNVjh4j/QdDX6+88grb\nbde8WHVIXJJGmZaWFo455hiWLVvWs+3uu+/mHe94B3vssQczZszgRz/6Uc++b33rW0yfPp2JEyfy\n2te+liuuuKJn+7ve9a6ecosWLeLAAw9k0qRJnH322Rs95tbe3s4ZZ5zRs97Z2cm4ceNYv349AJde\neikHHXQQEydOZPr06XzjG98YsP4XXngh++67LxMnTuTAAw/k9ttvH7DsQI/ajRs3jl/96ldArSd+\n1llncdxxxzFx4kQOP/zwnn0ADz/8MEcffTR77bUXBx54INdcc03PvhtvvJFDDz2U3XffnWnTpjFv\n3rxNvsdvfvObvOY1r2HmzObO2G1gS9IosSG8nnjiCW655Rbe/va3A/Dkk09y3HHHcd5557Fq1Sq+\n9KUvceKJJ/LMM8/w4osv8olPfIJbbrmF1atX8+Mf/5gZM2Zscuynn36aE088kS984Qs888wzTJ8+\nnbvuuqtnf72ebktLCzfeeCOrV6/m0ksv5ZOf/CT33XffJuUeeeQRLr74Yu69915Wr17NwoULaW1t\n3YJWqbn66qtpb29n1apVvO51r+Ozn/0sAC+++CJHH300p59+Or/5zW+46qqr+PjHP85DDz0EwIQJ\nE7j88st57rnnuPHGG/na177Gddddt9Gx77jjDh5++GFuvfXWLa7nYAxsSRoFMpPZs2czceJEpk2b\nxvTp0/nc5z4HwOWXX86sWbM49thjAZg5cyaHHXYYN954IxHBuHHjeOCBB/jtb39LS0sLBx100CbH\nv+mmm3jjG9/ICSecwPjx45k7dy5TpkzZ6PyDmTVrFvvvvz8A7373uznmmGO48847Nyk3fvx41qxZ\nw7Jly1i3bh3Tpk3jta997Wa3C9T+mDjhhBM47LDDGD9+PKeddhpLly4F4IYbbmD//fdnzpw5jBs3\njhkzZnDCCSf09LKPOOIIDj74YADe9KY3ccopp2w0OgG10YWdd96ZHXfccYvqWY+BLUmjQERw3XXX\nsXr1ajo6Orj99tu59957AXjssce45ppr2GOPPXped911FytWrGCXXXbh6quv5l/+5V/Ye++9Oe64\n43jkkUc2Of6vf/1r9t133422DeXO7ptvvpnDDz+cvfbaiz322IObbrqJZ555ZpNyr3vd65g/fz7t\n7e20tLRw6qmn8tRTTw2xNTbV0tLSs7zzzjvzwgsvALW2ueeeezZqmyuuuIKuri4A7rnnHo488khe\n/epXM2nSJL7+9a9vUu/hvsN9IAa2JI0y7373uzn77LP59Kc/DcC0adM444wzWLVqVc/r+eef51Of\n+hQAxxxzDAsXLmTFihUceOCBfPSjH93kmHvvvTePP/54z3pmbrQ+YcIEXnrppZ71FStW9CyvWbOG\nE088kU996lOsXLmSVatWMWvWrAF75aeeeip33nknjz32GBHR8330Z0tvOps2bRpHHHHEJm1z8cUX\nA/ChD32I2bNn88QTT9Dd3c3HPvaxnuvyw1WHRhnYkjQKzZ07lyVLlnDPPfdw+umn84Mf/ICFCxfy\nu9/9jpdffpmOjg6efPJJVq5cyXXXXceLL77I9ttvz6677sr48eM3Od6sWbNYtmwZ3//+93nllVe4\n6KKLNgrlGTNmcMcdd/D444/z3HPPcf755/fsW7t2LWvXrmXy5MmMGzeOm2++mYULF/Zb7+XLl3P7\n7bezZs0adtxxR3baaad+6wO1PxpeeeUVXn755Z7XunXr+i03kPe9730sX76cyy+/nHXr1rFu3Tp+\n8pOf8PDDDwPwwgsvsMcee7DDDjuwZMkSrrjiihG7M93AlqRRaPLkycyZM6fnjuvrrruOL3zhC7z6\n1a9m2rRpfPnLXyYzWb9+PV/5ylfYZ5992Guvvbjzzjv52te+Bmz82NTkyZO55pprOOecc5g8eTKP\nPvoo73znO3vON3PmTE4++WQOOeQQ3vrWt/L+97+/57277bYbF110ESeddBJ77rknV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O6r+vWiDiy\nz/667b1ZgZ2ZRw+2PyLOBGZRe0Z7gyeB/Xqt71tt0yDqtfUAbOvm6Nuu+7HxSIaGX1dETMnMFREx\nFVg50hUaLSJie2phfVlmXltttr2bLDOfi4gbgT9kiO3djLvEjwX+GvhAdaF9g+uBUyJih4jYH3g9\ntclWNDx6z5hjWzfHvcDrI6I1InagdmPf9SNcp9HuemBOtTwHuHaQsmpQ1LrSlwAPZub8Xrts7yaI\niMkb7gCPiJ2Bo4H7GGJ7D/tz2BHxC2oX1Z+tNv04Mz9e7fsMtevar1Abgrl1WE8+xkTEnwIXAZOB\n54D7MvNPqn22dRNExJ/w+8+AvyQzzx/hKo0aEXElcAS1/89dwHnAdcB3gGlAJ3BSZtb5QEbVU92h\nfAfwM35/qedcan/Y297DLCLeRO2msnHV67LM/IeI2JMhtLcTp0iSVACfzZUkqQAGtiRJBTCwJUkq\ngIEtSVIBDGxJkgpgYEuSVAADW5KkAhjYkiQV4P8D6ozDoP/R7QEAAAAASUVORK5CYII=\n",
      "text/plain": [
       "<matplotlib.figure.Figure at 0x75b8e48>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "%matplotlib inline\n",
    "from matplotlib import pyplot as plt\n",
    "f, ax = plt.subplots(figsize=(7, 5))\n",
    "f.tight_layout()\n",
    "ax.hist(boston.target-predictions,bins=40, label='Residuals Linear', color='b', alpha=.5);\n",
    "ax.set_title(\"Histogram of Residuals\")\n",
    "ax.legend(loc='best');"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "让我们看看相关系数："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ -1.07170557e-01,   4.63952195e-02,   2.08602395e-02,\n",
       "         2.68856140e+00,  -1.77957587e+01,   3.80475246e+00,\n",
       "         7.51061703e-04,  -1.47575880e+00,   3.05655038e-01,\n",
       "        -1.23293463e-02,  -9.53463555e-01,   9.39251272e-03,\n",
       "        -5.25466633e-01])"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "lr.coef_"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    ">一个常用的特征与对应的相关系数的呈现方式是用`zip`函数（Python 2.x用`zip`，Python 3.x里`zip`返回生成器，必须用`list(zip(...))`），这里是`zip(boston.feature_names, lr.coef_)`。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[('CRIM', -0.10717055656035571),\n",
       " ('ZN', 0.046395219529794252),\n",
       " ('INDUS', 0.020860239532177083),\n",
       " ('CHAS', 2.6885613993179542),\n",
       " ('NOX', -17.795758660309406),\n",
       " ('RM', 3.8047524602580065),\n",
       " ('AGE', 0.0007510617033257283),\n",
       " ('DIS', -1.4757587965198153),\n",
       " ('RAD', 0.30565503833909768),\n",
       " ('TAX', -0.012329346305270588),\n",
       " ('PTRATIO', -0.95346355469056121),\n",
       " ('B', 0.0093925127221890746),\n",
       " ('LSTAT', -0.52546663290078754)]"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "list(zip(boston.feature_names, lr.coef_))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "这样我们就可以看出哪个因素与因变量是负相关，哪个因素与因变量是正相关了。例如，上面的结果显示，波士顿地区的犯罪率与房价负相关。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##How it works..."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "线性回归的基本理念是找出满足$y=X\\beta$的相关系数集合$\\beta$，其中$X$是因变量数据矩阵。想找一组完全能够满足等式的相关系数很难，因此通常会增加一个误差项表示不精确程度或测量误差。因此，方程就变成了$y=X\\beta+\\epsilon$，其中$\\epsilon$被认为是服从正态分布且与$X$独立的随机变量。用几何学的观点描述，就是说这个变量与$X$是正交的（perpendicular）。这超出了本书的范围，可以参考其他信息证明$E(X\\epsilon)=0$。\n",
    "\n",
    "为了找到相关系数集合$\\beta$，我们最小化误差项，这转化成了残差平方和最小化问题。\n",
    "\n",
    "这个问题可以用解析方法解决，其解是$\\beta=(X^TX)^{-1}X^T \\hat y$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "###There's more..."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "`LinearRegression`对象可以自动标准正态化（normalize或scale）输入数据："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "LinearRegression(copy_X=True, fit_intercept=True, n_jobs=1, normalize=True)"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "lr2 = LinearRegression(normalize=True)\n",
    "lr2.fit(boston.data, boston.target)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "predictions2 = lr2.predict(boston.data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "collapsed": false,
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAewAAAFsCAYAAAAOmoq4AAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAIABJREFUeJzt3XucnVV97/HPL+FOCAGik3CJg1GKoBgsKrUqAwakETEF\ny0WgwePLvqwcajznVEE9dFJbAasvU1q02iKmIBfRCsg1OeAIoUKkEsRwiegZDiCZCGQIF0mC+Z0/\n9pNxMpmZvSeZncma+bxfr/2a57L286xZycx31nqeZ+3ITCRJ0rZt3EhXQJIk1WdgS5JUAANbkqQC\nGNiSJBXAwJYkqQAGtiRJBTCwNaZF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kk09y3HHHcd5557Fq1Sq+\n9KUvceKJJ/LMM8/w4osv8olPfIJbbrmF1atX8+Mf/5gZM2Zscuynn36aE088kS984Qs888wzTJ8+\nnbvuuqtnf72ebktLCzfeeCOrV6/m0ksv5ZOf/CT33XffJuUeeeQRLr74Yu69915Wr17NwoULaW1t\n3YJWqbn66qtpb29n1apVvO51r+Ozn/0sAC+++CJHH300p59+Or/5zW+46qqr+PjHP85DDz0EwIQJ\nE7j88st57rnnuPHGG/na177Gddddt9Gx77jjDh5++GFuvfXWLa7nYAxsSRoFMpPZs2czceJEpk2b\nxvTp0/nc5z4HwOWXX86sWbM49thjAZg5cyaHHXYYN954IxHBuHHjeOCBB/jtb39LS0sLBx100CbH\nv+mmm3jjG9/ICSecwPjx45k7dy5TpkzZ6PyDmTVrFvvvvz8A7373uznmmGO48847Nyk3fvx41qxZ\nw7Jly1i3bh3Tpk3jta997Wa3C9T+mDjhhBM47LDDGD9+PKeddhpLly4F4IYbbmD//fdnzpw5jBs3\njhkzZnDCCSf09LKPOOIIDj74YADe9KY3ccopp2w0OgG10YWdd96ZHXfccYvqWY+BLUmjQERw3XXX\nsXr1ajo6Orj99tu59957AXjssce45ppr2GOPPXped911FytWrGCXXXbh6quv5l/+5V/Ye++9Oe64\n43jkkUc2Of6vf/1r9t133422DeXO7ptvvpnDDz+cvfbaiz322IObbrqJZ555ZpNyr3vd65g/fz7t\n7e20tLRw6qmn8tRTTw2xNTbV0tLSs7zzzjvzwgsvALW2ueeeezZqmyuuuIKuri4A7rnnHo488khe\n/epXM2nSJL7+9a9vUu/hvsN9IAa2JI0y7373uzn77LP59Kc/DcC0adM444wzWLVqVc/r+eef51Of\n+hQAxxxzDAsXLmTFihUceOCBfPSjH93kmHvvvTePP/54z3pmbrQ+YcIEXnrppZ71FStW9CyvWbOG\nE088kU996lOsXLmSVatWMWvWrAF75aeeeip33nknjz32GBHR8330Z0tvOps2bRpHHHHEJm1z8cUX\nA/ChD32I2bNn88QTT9Dd3c3HPva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"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "%matplotlib inline\n",
    "from matplotlib import pyplot as plt\n",
    "f, ax = plt.subplots(figsize=(7, 5))\n",
    "f.tight_layout()\n",
    "ax.hist(boston.target-predictions2,bins=40, label='Residuals Linear', color='b', alpha=.5);\n",
    "ax.set_title(\"Histogram of Residuals\")\n",
    "ax.legend(loc='best');"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ -6.68134034,  15.32488183])"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.percentile(boston.target-predictions2, [2.5, 99])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ -6.68134034,  15.32488183])"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.percentile(boston.target-predictions, [2.5, 99])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "与前面的结果没有不同。"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
